//
//  DiscountViewController.m
//  QYer
//
//  Created by lanou on 16/8/8.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "DiscountViewController.h"

#import "DaySpecialDiscountModel.h"
#import "HotGoodsModel.h"

#import "DiscountHeaderCell.h"
#import "DaySpecialDiscountCell.h"
#import "HotGoodsReusableView.h"
#import "HotGoodsCollectionViewCell.h"

#import "ShopListViewController.h"

@interface DiscountViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;


@property (nonatomic, strong) NSArray *titleArray;
//天天特价
@property (nonatomic, strong) NSMutableArray *dayDicountArray;
//有好货
@property (nonatomic, strong) NSMutableArray *hotGoodsArray;

@end

@implementation DiscountViewController


- (void)requestData {
    [NetWorkTool requstWithType:GET URLString:discount parameter:nil callBack:^(NSData *data, NSError *error) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //jsonDic[@"data"][@"market_topic"]--->天天特价
        for (NSDictionary *dic in jsonDic[@"data"][@"market_topic"]) {
            DaySpecialDiscountModel *model = [[DaySpecialDiscountModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dayDicountArray addObject:model];
        }
        //jsonDic[@"data"][@"hot_area"]---->东南亚,欧美,港澳台
        /*
        NSArray *areaArray = jsonDic[@"data"][@"hot_area"];
        for (int i = 0; i < areaArray.count; i++) {
            NSDictionary *dictionary = areaArray[i];
            for (NSDictionary *dic in dictionary[@"place"]) {
                //
            }
            for (NSDictionary *dic in dictionary[@"list"]) {
                //
            }
        }
         */
        //jsonDic[@"data"][@"discount_topic"]---->不愁去哪玩,机票
        /*
        NSArray *discountArray = jsonDic[@"data"][@"discount_topic"];
        for (int i = 0; i < discountArray.count; i++) {
            NSDictionary *dictionary = discountArray[i];
            for (NSDictionary *dic in dictionary[@"topic"]) {
                //
            }
            for (NSDictionary *dic in dictionary[@"list"]) {
                //
            }
        }
        */
        
        //jsonDic[@"data"][@"hot_goods"]---->有好货
        for (NSDictionary *dic in jsonDic[@"data"][@"hot_goods"]) {
            HotGoodsModel *model = [[HotGoodsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.hotGoodsArray addObject:model];
        }
        
        //NSLog(@"天天特价:%@", self.dayDicountArray);
        //NSLog(@"有好货:%@", self.hotGoodsArray);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }];
}

- (void)createSubView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"DiscountHeaderCell" bundle:nil] forCellWithReuseIdentifier:@"headerReuse"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"DaySpecialDiscountCell" bundle:nil] forCellWithReuseIdentifier:@"DiscountReuse"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HotGoodsReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GoodsHeaderReuse"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HotGoodsCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"GoodsReuse"];
}


#pragma mark -----

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"旅行商城";
    
    self.dayDicountArray = @[].mutableCopy;
    self.hotGoodsArray = @[].mutableCopy;
    
    [self requestData];
    [self createSubView];
    
    self.titleArray = @[@"机酒套餐", @"特价机票", @"酒店", @"机票搜索", @"签证", @"当地游", @"门票", @"WiFi电话", @"交通票券", @"接送包车"];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark -----  UICollectionViewDataSource -----

//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return self.titleArray.count;
    } else if (section == 1) {
        return self.dayDicountArray.count;
    } else {
        return self.hotGoodsArray.count;
    }
}
//item样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DiscountHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
        cell.picView.image = [UIImage imageNamed:self.titleArray[indexPath.row]];
        cell.titleLabel.text = self.titleArray[indexPath.row];
        return cell;
    } else if (indexPath.section == 1) {
        DaySpecialDiscountCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscountReuse" forIndexPath:indexPath];
        DaySpecialDiscountModel *model = self.dayDicountArray[indexPath.row];
        [cell.PicView sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"占位图"]];
        return cell;
    } else {
        HotGoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsReuse" forIndexPath:indexPath];
        [cell setContentWithModel:self.hotGoodsArray[indexPath.row]];;
        return cell;
    }
}
//item尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake((kScreenWidth-60)/5, (kScreenWidth-60)/5+30);
    } else if (indexPath.section == 1) {
        return CGSizeMake(kScreenWidth - 20, 200);
    } else {
        return CGSizeMake((kScreenWidth-50)/2, 250);
    }
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld %ld", indexPath.section, indexPath.row);
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //机酒套餐
            ShopListViewController *shopListVC = [[ShopListViewController alloc] init];
            shopListVC.typeID = @"146";
            [self.navigationController pushViewController:shopListVC animated:YES];
        } else if (indexPath.row == 1) {
            //特价机票
            ShopListViewController *shopListVC = [[ShopListViewController alloc] init];
            shopListVC.typeID = @"145";
            [self.navigationController pushViewController:shopListVC animated:YES];
        } else if (indexPath.row == 2) {
            //酒店
#warning //
        } else if (indexPath.row == 3) {
            //机票搜索
#warning //
        } else if (indexPath.row == 4) {
            //签证
            ShopListViewController *shopListVC = [[ShopListViewController alloc] init];
            shopListVC.typeID = @"149";
            [self.navigationController pushViewController:shopListVC animated:YES];
        } else if (indexPath.row == 5) {
            //当地游
            ShopListViewController *shopListVC = [[ShopListViewController alloc] init];
            shopListVC.typeID = @"173";
            [self.navigationController pushViewController:shopListVC animated:YES];
        } else if (indexPath.row == 6) {
            //门票
            ShopListViewController *shopListVC = [[ShopListViewController alloc] init];
            shopListVC.typeID = @"160";
            [self.navigationController pushViewController:shopListVC animated:YES];
        } else if (indexPath.row == 7) {
            //WiFi电话
            ShopListViewController *shopListVC = [[ShopListViewController alloc] init];
            shopListVC.typeID = @"163";
            [self.navigationController pushViewController:shopListVC animated:YES];
        } else if (indexPath.row == 8) {
            //交通票券
            ShopListViewController *shopListVC = [[ShopListViewController alloc] init];
            shopListVC.typeID = @"175";
            [self.navigationController pushViewController:shopListVC animated:YES];
        } else {
            //接送包车
            ShopListViewController *shopListVC = [[ShopListViewController alloc] init];
            shopListVC.typeID = @"174";
            [self.navigationController pushViewController:shopListVC animated:YES];
        }
        
    } else if (indexPath.section == 1) {
        //
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        DaySpecialDiscountModel *model = self.dayDicountArray[indexPath.row];
        detailVC.urlString = model.url;
        [self.navigationController pushViewController:detailVC animated:YES];
    } else {
        //http://m.qyer.com/z/deal/70042/?source=app&client_id=qyer_ios&track_app_version=7.0&track_deviceid=2D9E8D6C-2140-49DC-993D-D66133CC22C0&track_user_id=8252405
        
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        HotGoodsModel *model = self.hotGoodsArray[indexPath.row];
        NSString *urlString = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=7.0&track_deviceid=2D9E8D6C-2140-49DC-993D-D66133CC22C0&track_user_id=8252405", model.ID];
        detailVC.urlString = urlString;
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }
}



//分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
//分区头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    HotGoodsReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GoodsHeaderReuse" forIndexPath:indexPath];
    if (indexPath.section == 2) {
        view.titleLabel.text = @"有好货";
    } else {
        view.titleLabel.text = nil;
    }
    return view;
    
}

//分区头尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return CGSizeMake(kScreenWidth, 50);
    } else {
        return CGSizeMake(kScreenWidth, 10);
    }
}


//设置collectionView当前页距离四周的边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}








@end
